package com.rc.saas.mini.controller.format.qrcode;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 万马二维码获取
 *type=cdz&code=3301061019321443&q=12(二维码电桩q=12后面有可能会有乱码)
 * 	字段描述：其中 code表示：电桩编号，q表示枪口号, 其中:( type=cdz&):是不变的，每个电桩枪口二维码都是唯一的
 * Ps:由于电桩版本不一致部分电桩会在q=12后面有乱码信息,因此扫描二维码后截取拼接, ConnectorID时,截取”q=”后面两位值就好,后面不需要关注,
 * 	调用启动充电接口时字段：ConnectorID= String.format("%s%02d",code, q)，
 * 	示例:ConnectorID=3301061019321443012,二维码规则为：type=cdz&code=3301061019321443&q=12，
 * 其中：q=1 表示实际电桩A枪，B枪就是q=2,其他类似
 *  * @author wangzhengwei
 *  * @create 2021/5/11
 */
public class WmQrCodeFormat extends QrCodeFormat {
    private static Logger logger = LogManager.getLogger(WmQrCodeFormat.class);

    @Override
    public String getGunNoByQrCode(String qrCode) {
        return notify(qrCode);
    }


    public String notify(String qrCode) {
        //是否包含 &connectorSn=
        //type=cdz&code=3301061019321443&q=12
        if (!qrCode.contains("type=cdz&code=")) {
            return null;
        }else{
            String reg = "type=cdz&code=(\\S+)&q=(\\d+)";
            Pattern pattern = Pattern.compile(reg.trim());
            Matcher matcher = pattern.matcher(qrCode);
            if (matcher.find()) {
                if(matcher.group(2).length()>2){
                    return matcher.group(1)+matcher.group(2).substring(0,2);
                }else{
                    return matcher.group(1)+String.format("%02d",Integer.valueOf(matcher.group(2)));
                }

            }
        }



        return null;
    }


    public static void main(String[] args) {
        //String code="type=cdz&code=3301061019321443&q=0145556乱码";
        //String code="type=cdz&code=3301061019321443&q=1";
        //String code="type=cdz&code=3301061019321443&q=01";
        String code="type=cdz&code=3301061019321443&q=1zzzzzzz乱码";

        String reg = "type=cdz&code=(\\S+)&q=(\\d+)";
        Pattern pattern = Pattern.compile(reg.trim());
        Matcher matcher = pattern.matcher(code);
        if (matcher.find()) {
            System.out.println(matcher.group(2));
            if(matcher.group(2).length()>2){
                System.out.println(matcher.group(1)+matcher.group(2).substring(0,2));
            }else{
                System.out.println(matcher.group(1)+String.format("%02d",Integer.valueOf(matcher.group(2))));
            }

            //System.out.println( matcher.group(1)+matcher.group(2).substring(0,2));
        }


    }
}
